<?php

function rank_add() {
	global $conf;

	SQLvalidate($_POST['path'], 'varchar');
	SQLvalidate($_POST['posts'], 'int', 8);

	if(!perms_check('users', 'gr_edit')) {
		plugins('std/unauth/');
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	if(!$_POST['path'] or !is_numeric($_POST['points'])) {
		redirect($_SESSION['redirect_1']);
		exit();
	}

	$db = new dbquery;

	$query = "INSERT INTO $conf[prefix_ranks]ranks VALUES(NULL, '$_POST[path]', $_POST[points] ";

	$languages = get_lang_list();
	foreach($languages as $l) {
		SQLvalidate($_POST["name_$l"], 'varchar');
		$_POST["name_$l"] = post_text($_POST["name_$l"]);
		$query .= ", '".$_POST["name_$l"]."'";
	}

	$query .= ')';

	$db->query($query) or $db->err(__FILE__, __LINE__);

	//add log
	
	//

	$db->clear_cache('ranks.cache', 'ranks');

	redirect("index.php?module=admin&amp;action=ranks");
	exit();
}


function rank_edit() {
	global $conf;

	SQLvalidate($_POST['id']);
	SQLvalidate($_POST['path'], 'varchar');
	SQLvalidate($_POST['points'], 'int', 8);

	$db = new dbquery;

	if(!perms_check('users', 'gr_edit')) {
		plugins('std/unauth/');
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	if(!$_POST['id'] or !$_POST['path'] or !is_numeric($_POST['points'])) {
		redirect($_SESSION['redirect_1']);
		exit();
	}

	$db->query("SELECT * FROM $conf[prefix_ranks]ranks WHERE id = $_POST[id]") or $db->err(__FILE__, __LINE__);

	if($db->num_rows() == 0) {
		redirect($_SESSION['redirect_1']);
		exit();
	}

	$d = $db->fetch_object();

	$query = "UPDATE $conf[prefix_ranks]ranks SET path='$_POST[path]', points=$_POST[points]";

	$languages = get_lang_list();
	foreach($languages as $l) {
		SQLvalidate($_POST["name_$l"], 'varchar');
		$_POST["name_$l"] = post_text($_POST["name_$l"]);
		$query .= ", name_$l = '".$_POST["name_$l"]."'";
	}

	$query .= " WHERE id=$_POST[id]";

	$db->query($query) or $db->err(__FILE__, __LINE__);

	//add log
	
	//

	$db->clear_cache('ranks.cache', 'ranks');
	$db->clear_cache('rank_'.$_POST['id'].'.cache', 'ranks');

	redirect("index.php?module=admin&amp;action=ranks");
	exit();
}

function rank_delete() {
	SQLvalidate($_GET['rank']);
	if(!perms_check('users', 'gr_edit')) {
		plugins('std/unauth/');
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	global $conf;

	$db = new dbquery;

	if(!$_GET['rank'] or !is_numeric($_GET['rank'])) {
		redirect($_SESSION['redirect_1']);
		exit();
	}

	$db->query("SELECT * FROM $conf[prefix_ranks]ranks WHERE id = $_GET[rank]") or $db->err(__FILE__, __LINE__);

	if($db->num_rows() == 0) {
		redirect($_SESSION['redirect_1']);
		exit();
	}

	$db->query("DELETE FROM $conf[prefix_ranks]ranks WHERE id = $_GET[rank]") or $db->err(__FILE__, __LINE__);

	$db->query("UPDATE $conf[prefix_groups]groups SET rank = 0 WHERE rank = $_GET[rank]") or $db->err(__FILE__, __LINE__);

	//add log
	
	//

	$db->cache_clear('rank_'.$_GET['rank'].'.cache', 'ranks');
	$db->cache_clear('rank_post_*', 'ranks');

	redirect("index.php?module=admin&amp;action=ranks");
	exit();
}
?>
